package com.ruoyi.project.configureSystem.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.configureSystem.domain.SysArea;
import com.ruoyi.project.configureSystem.service.ISysAreaService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 区域信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/configureSystem/varea")
public class SysAreaController extends BaseController
{
    @Autowired
    private ISysAreaService areaService;
    @Autowired
    private SysUserServiceImpl sysUserService;


    /**
     * 获取区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:varea:list')")
    @GetMapping("/list")
    public AjaxResult list(SysArea area)
    {
        List<SysArea> Areas = areaService.selectAreaList(area);
        return AjaxResult.success(Areas);
    }

    /**
     * 根据区域编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:varea:query')")
    @GetMapping(value = "/{areaId}")
    public AjaxResult getInfo(@PathVariable Long areaId)
    {
        return AjaxResult.success(areaService.selectAreaById(areaId));
    }

    /**
     * 获取区域下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysArea area)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        area.setUserId(loginUser.getUser().getUserId());
        List<SysArea> areas = areaService.selectAreaList(area);
        return AjaxResult.success(areaService.buildAreaTreeSelect(areas));
    }

    /**
     * 根据用户角色-获取区域下拉树列表
     */
    @GetMapping("/treeselectByuserRole")
    public AjaxResult treeselectByuserRole(SysArea area)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        area.setUserId(loginUser.getUser().getUserId());
        List<SysArea> areas = areaService.selectAreaList(area);
        return AjaxResult.success(areaService.buildAreaTreeSelect(areas));
    }


    /**
     * 加载对应角色区域列表树
     */
    @GetMapping(value = "/roleAreaTreeselect/{roleId}")
    public AjaxResult roleAreaTreeselect(@PathVariable("roleId") Long roleId)
    {
        List<SysArea> areas = areaService.selectAreaList(new SysArea());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", areaService.selectAreaListByRoleId(roleId));
        ajax.put("areas", areaService.buildAreaTreeSelect(areas));
        return ajax;
    }

    /**
     * 新增区域
     */
    @PreAuthorize("@ss.hasPermi('system:varea:add')")
    @Log(title = "区域管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysArea area)
    {
        if (UserConstants.NOT_UNIQUE.equals(areaService.checkAreaNameUnique(area)))
        {
            return AjaxResult.error("新增区域'" + area.getAreaName() + "'失败，区域名称已存在");
        }
        area.setCreateBy(SecurityUtils.getUsername());
        return toAjax(areaService.insertArea(area));
    }

    /**
     * 修改区域
     */
    @PreAuthorize("@ss.hasPermi('system:varea:edit')")
    @Log(title = "区域管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysArea area)
    {
        if (UserConstants.NOT_UNIQUE.equals(areaService.checkAreaNameUnique(area)))
        {
            return AjaxResult.error("修改区域'" + area.getAreaName() + "'失败，区域名称已存在");
        }
        else if (area.getParentId().equals(area.getAreaId()))
        {
            return AjaxResult.error("修改区域'" + area.getAreaName() + "'失败，上级区域不能是自己");
        }
        area.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(areaService.updateArea(area));
    }

    /**
     * 删除区域
     */
    @PreAuthorize("@ss.hasPermi('system:varea:remove')")
    @Log(title = "区域管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{areaId}")
    public AjaxResult remove(@PathVariable Long areaId)
    {
        if (areaService.hasChildByAreaId(areaId))
        {
            return AjaxResult.error("存在下级区域,不允许删除");
        }
        if (areaService.checkAreaExistUser(areaId))
        {
            return AjaxResult.error("区域存在用户,不允许删除");
        }
        return toAjax(areaService.deleteAreaById(areaId));
    }
}
